/////////////////////////////////////////////////////////////////////////////
// Name:        maintoolbar.cpp
// Purpose:     
// Author:      Brant Young
// Modified by: 
// Created:     Mon May 14 21:46:51 2007
// RCS-ID:      
// Copyright:   Copyright (c) daodict.com 2007
// Licence:     GPL 2.0
/////////////////////////////////////////////////////////////////////////////
#include <wx/wx.h>
#include <wx/srchctrl.h>
#include <wx/tokenzr.h>

#include "chmfindtoolbar.h"
#include "chmhtmlwnd.h"

/**
 * CHMFindToolBar constructors
 */
CHMFindToolBar::CHMFindToolBar(wxWindow* p_parent, CHMHtmlWindow* p_to_search, wxBitmap& close_bmp, wxBitmap& find_next_bmp) : \
					wxToolBar(p_parent, ID_FIND_TOOLBAR, \
	                       wxDefaultPosition, wxDefaultSize, \
	                       wxTB_FLAT|wxTB_HORIZONTAL|wxTB_HORZ_TEXT | \
	                       wxTB_NODIVIDER),
	                _p_html_wnd(p_to_search), _p_cell(NULL)
{
    this->SetToolBitmapSize(wxSize(16, 16));
    // ThemeManager& theme_mgr = ThemeManager::get();
    

    // -----------------------------------------------------------

    // wxBitmap& bmp = theme_mgr.get_bitmap(_T("close-16.png"));
    this->AddTool(ID_CLOSE, _T("Close"), close_bmp, wxNullBitmap, wxITEM_NORMAL, _("Close"));
    
    //wxStaticText* p_find_stc = new wxStaticText(this, ID_FIND_TEXT, _T(" Find: "), wxDefaultPosition, wxSize(-1, -1), 0);
    //this->AddControl(p_find_stc);
    this->AddSeparator();

    p_find_srchctrl = new wxSearchCtrl(this, ID_FIND_SRCHCTRL, _T(""), wxDefaultPosition, wxSize(160, -1), wxTE_PROCESS_ENTER);
    p_find_srchctrl->SetDescriptiveText(_("Find"));
    this->AddControl(p_find_srchctrl);
    
    this->AddSeparator();
    
    //p_find_text = new wxTextCtrl(this, ID_FIND_TEXT, _T(""), wxDefaultPosition, wxSize(130, -1), 0);
    //this->AddControl(p_find_text);

    // bmp = theme_mgr.get_bitmap(_T("find-next-16.png"));
    this->AddTool(ID_NEXT, _T("Next"), find_next_bmp, wxNullBitmap, wxITEM_NORMAL, _("Find Next"));

    // bmp = theme_mgr.get_bitmap(_T("find-previous-16.png"));
    // this->AddTool(ID_PREVIOUS, _T(""), bmp, wxNullBitmap, wxITEM_NORMAL, _("Find Previous"));

    this->AddSeparator();
    
    p_match_case_chkbox = new wxCheckBox( this, ID_MATCH_CASE_CHKBOX, _("Mat&ch case"), wxDefaultPosition, wxDefaultSize, 0 );
    p_match_case_chkbox->SetValue(false);
    this->AddControl(p_match_case_chkbox);
    
    this->Realize();
    this->InitEvents();
}


/**
 * CHMFindToolBar destructor
 */

CHMFindToolBar::~CHMFindToolBar()
{
////@begin CHMFindToolBar destruction
////@end CHMFindToolBar destruction
}


void CHMFindToolBar::InitEvents() {
	this->Connect(ID_CLOSE, wxEVT_COMMAND_TOOL_CLICKED, 
			(wxObjectEventFunction)&CHMFindToolBar::OnClose);
	
	this->Connect(ID_NEXT, wxEVT_COMMAND_TOOL_CLICKED, 
			(wxObjectEventFunction)&CHMFindToolBar::OnFindNext);
	
	this->Connect(ID_FIND_SRCHCTRL, wxEVT_COMMAND_TEXT_ENTER, 
			(wxObjectEventFunction)&CHMFindToolBar::OnFindNext);
}


void CHMFindToolBar::OnClose(wxCommandEvent& event) {
	if (true==IsShown()) {
		//this->Realize();
		Hide();
	} else {
		Show();
	}
	
	this->GetParent()->Layout();
}


void CHMFindToolBar::OnFindNext(wxCommandEvent& event) {
	find_forward();
}


void CHMFindToolBar::find_forward() {
	_p_html_wnd->ClearSelection();

	wxString sr = p_find_srchctrl->GetLineText(0);
	if (sr.IsEmpty())
		return;

	wxStringTokenizer tkz(sr, wxT(" \t\r\n"));
	wxString word;

	while(word.IsEmpty())
		if(tkz.HasMoreTokens())
			word = tkz.GetNextToken();

	if(!p_match_case_chkbox->IsChecked())
		word.MakeLower();

	if(!_p_cell || word.Cmp(_currWord.c_str())) {
		_p_cell = _p_html_wnd->FindFirst(_p_html_wnd->GetInternalRepresentation(),
					 word, false,
					 p_match_case_chkbox->IsChecked());
		_currWord = word;

	} else {

		if(_p_cell && _p_cell->GetNext()) 
			_p_cell = _p_cell->GetNext();
	        else {
			while(_p_cell && !_p_cell->GetNext())
				_p_cell = _p_cell->GetParent();

			if(_p_cell)
				_p_cell = _p_cell->GetNext();
		}

		if(!_p_cell)
			return;

		_p_cell = _p_html_wnd->FindNext(_p_cell, word, false, 
				       p_match_case_chkbox->IsChecked());

		// Wrap around.
		if(!_p_cell) {
			_p_cell = _p_html_wnd->FindFirst(
				_p_html_wnd->GetInternalRepresentation(),
				word, false,
				p_match_case_chkbox->IsChecked());
		}
	}
}
